<?php
/** 
 * This Queue will get The Total No. of Ordering 
 * Que is what you should get 
 * There should another back order Queue , when Order turn into shipment , the order can not fullfilled
 * the Back order Que is filled / Cleared. that Que is How work been done Que this Que is how many you should get Que
 * 
 * @author Norman
 *
 */

class Model_DbTable_Order_Queue extends Zend_Db_Table_Abstract {

	protected $_name = 'orders_v1_queue';
	
	public function getQueue( $idQue){
		
		$row = $this->fetchRow("`id_que` = ". $idQue);
		if(!$row) return false;
		return $row->toArray();		
		
		}
		
	public function addQueue( $codeProduct , $desProduct , $qtyOrder , $dateCreate , $dateUpdate , $comment , $active){
		
		$data = array(
			
         "code_product" =>  $codeProduct ,
         "des_product" =>  $desProduct ,
         "qty_order" =>  $qtyOrder ,
         "date_create" =>  $dateCreate ,
         "date_update" =>  $dateUpdate ,
         "comment" =>  $comment ,
         "active" =>  $active 
	
			);
		$this->insert($data);
		
		}
	public function conditionalAddQueue($codeProduct,$desProduct,$qtyOrder,$comment,$active){
		if(self::codeExist($codeProduct)){
			$idLine = self::codeExist($codeProduct);
			$qtyNow = $idLine['qty_order'];
			self::updateProductQty($codeProduct, $qtyOrder + $qtyNow);
		}
		else{
			
			self::addQueue($codeProduct, $desProduct, $qtyOrder,Model_DatetimeHelper::dateToday(),Model_DatetimeHelper::dateToday(), $comment, $active);
		}
		
		
	}
	
	public function conditionalFullfillQueue($codeProduct,$qtyReceived){
		if(self::codeExist($codeProduct)){
			$idLine = self::codeExist($codeProduct);
			$qtyNow = $idLine['qty_order'];
			if($qtyReceived >= $qtyNow){
				self::deleteQueue($idLine['id_que']);
			}else{
				
				self::updateProductQty($codeProduct, $qtyNow - $qtyReceived);
			}
			
		}
		
		
	}
		
	public function updateQueue(  $idQue ,  $codeProduct , $desProduct , $qtyOrder , $dateCreate , $dateUpdate , $comment , $active){
		$data = array(
			
         "code_product" =>  $codeProduct ,
         "des_product" =>  $desProduct ,
         "qty_order" =>  $qtyOrder ,
         "date_create" =>  $dateCreate ,
         "date_update" =>  $dateUpdate ,
         "comment" =>  $comment ,
         "active" =>  $active 
	
			);
			
		$this->update($data,"`id_que` = ". $idQue);
		}
		
	public function deleteQueue( $idQue){
		
		$this->delete("`id_que` = ". $idQue);
		
		}	
	
	public function listAll(){
		
		$rows =$this->fetchAll("1","id_que DESC ");
		if(!$rows) return false;
		return $rows->toArray();			
		
		}	
		
	public function updateProductQty($codeProduct,$qtyOrder){
		$data = array(
				"qty_order" =>  $qtyOrder ,
				"date_update" =>  Model_DatetimeHelper::dateToday() ,
		);
			
		$this->update($data,"`code_product` LIKE '".$codeProduct."'");
		
	}	
	
	public function codeExist($codeProduct){
		$row = $this->fetchRow("`code_product` LIKE '".$codeProduct."'");
		if(!$row) return false;
		return $row->toArray();
		
	}
}


?>